package com.itrus.contract.extension.shardinsphere.util;


import com.itrus.contract.extension.shardinsphere.enumeration.TableRulesEnum;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ShardingDateValueUtil {

    public static String getPostfix(Integer shardingDate, TableRulesEnum rulesEnum){
        if (Objects.isNull(shardingDate) || shardingDate==0 || Objects.isNull(rulesEnum)){
            return "";
        }
        LocalDate localDate = LocalDate.parse(String.valueOf(shardingDate), DateTimeFormatter.ofPattern("yyyyMMdd"));
        switch (rulesEnum){
            case NORMAL: return "";
            default:
                return localDate.format(DateTimeFormatter.ofPattern(rulesEnum.getFormat()));
        }
    }

    public static List<String> calcPostfixList(Integer beginDate, Integer endDate, TableRulesEnum rulesEnum){
        List<String> postfixList = new ArrayList<>();
        if (Objects.isNull(beginDate) || Objects.isNull(endDate) || Objects.isNull(rulesEnum)
                || TableRulesEnum.NORMAL.equals(rulesEnum)){
            return postfixList;
        }
        LocalDate begin = LocalDate.parse(String.valueOf(beginDate), DateTimeFormatter.ofPattern("yyyyMMdd"));
        LocalDate end = LocalDate.parse(String.valueOf(endDate), DateTimeFormatter.ofPattern("yyyyMMdd"));
        if (begin.isAfter(end)){
            return postfixList;
        }

        String endFormat = end.format(DateTimeFormatter.ofPattern(rulesEnum.getFormat()));

        postfixList.add(begin.format(DateTimeFormatter.ofPattern(rulesEnum.getFormat())));
        while (begin.isBefore(end)){
            switch (rulesEnum){
                case YEAR: begin = begin.plusYears(1);break;
                case MONTH: begin = begin.plusMonths(1);break;
                case DAY: begin = begin.plusDays(1);break;
            }
            String postfix = begin.format(DateTimeFormatter.ofPattern(rulesEnum.getFormat()));
            if (Integer.parseInt(postfix) <= Integer.parseInt(endFormat)){
                postfixList.add(postfix);
            }
        }
        return postfixList;
    }

    public static LocalDate minFillDate(String date, TableRulesEnum rulesEnum){
        String localDate = date;
        switch (rulesEnum){
            case YEAR:
                if (date.length()==2){
                    localDate = "20"+date+"0101";
                } else {
                    localDate = date+"0101";
                }
                break;
            case MONTH:
                if (date.length()==4){
                    localDate = "20"+date+"01";
                } else {
                    localDate = date+"01";
                }
                break;
        }
        return LocalDate.parse(localDate,DateTimeFormatter.BASIC_ISO_DATE);
    }
    public static LocalDate maxFillDate(String date, TableRulesEnum rulesEnum){
        return minFillDate(date, rulesEnum).with(TemporalAdjusters.lastDayOfMonth());
    }
    public static Integer maxFillDateToInt(String date, TableRulesEnum rulesEnum){
        return Integer.parseInt(maxFillDate(date, rulesEnum).format(DateTimeFormatter.BASIC_ISO_DATE));
    }

}
